//
//  CollectViewController.m
//  一点互助
//
//  Created by 张泊秋 on 16/8/20.
//  Copyright © 2016年 zhangboqiu. All rights reserved.
//

#import "CollectViewController.h"
#import "CollectCustomCell.h"
#import "ResponseManager.h"
#import "WishesModel.h"
#import "DetailViewController.h"


static NSString *collectIdentifier = @"collectTableViewCell";
@interface CollectViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *dataSoure;

@end

@implementation CollectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"收藏";
    self.tableView.estimatedRowHeight = 30;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    [self setupBackItem];
    [self.view addSubview:self.tableView];
}

- (void)viewWillAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = YES;
    
    [ResponseManager startGetRequestWithURLString:kCollectURL parameters:nil completionHandler:^(FBResponse *response) {
        if (response.success) {
            NSArray *arry = [WishesModel modelArrayWithDictionaryArr:response.resultValue];
            
            response.resultValue = arry;
            
            self.dataSoure = response.resultValue;
            NSLog(@"收藏 = %@",self.dataSoure);
            [self.tableView reloadData];
           
        }else {
            NSLog(@"%@",response.resultDesc);
        }
        
    }];
    
}
- (void)viewWillDisappear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = NO;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSoure.count;
}

//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    return 140 * kScreenHeightScale;
//}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.1 * kScreenHeightScale;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CollectCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:collectIdentifier];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.model = self.dataSoure[indexPath.row];

    return cell;
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    DetailViewController *detailVC = [[DetailViewController alloc]init];
    detailVC.model = self.dataSoure[indexPath.row];
    [self.navigationController pushViewController:detailVC animated:YES];
}


- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        UINib *nib = [UINib nibWithNibName:@"CollectCustomCell" bundle:nil];
        [_tableView registerNib:nib forCellReuseIdentifier:collectIdentifier];
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.dataSource = self;
        _tableView.delegate = self;
    }
    
    return _tableView;
}

- (NSArray *)dataSoure {
    if (!_dataSoure) {
        _dataSoure = [NSMutableArray array];
    }
    return _dataSoure;
}

@end
